package com.fujiyuu75.sequent;

import ohos.agp.animation.AnimatorGroup;
import ohos.agp.components.Component;

/**
 * Created by y_fujikawa on 2017/05/26.
 */

public enum Animation {

    BOUNCE_IN(1),
    FADE_IN(2),
    FADE_IN_DOWN(3),
    FADE_IN_UP(4),
    FADE_IN_LEFT(5),
    FADE_IN_RIGHT(6),
    ROTATE_IN(7);

    private int animId;

    private AnimatorGroup bounceIn(Component component) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runParallel(component.createAnimatorProperty().alphaFrom(0.5f).alpha(1.0f).setDuration(500),
                component.createAnimatorProperty().scaleXFrom(0.0f).scaleX(1.0f).setDuration(500),
                component.createAnimatorProperty().scaleYFrom(0.0f).scaleY(1.0f).setDuration(500));
        return animatorGroup;
    }

    private AnimatorGroup fadeIn(Component component) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runSerially(component.createAnimatorProperty().alphaFrom(0.0f).alpha(0.0f).setDuration(100),
                component.createAnimatorProperty().alphaFrom(0.0f).alpha(1.0f).setDuration(500));
        return animatorGroup;
    }

    private AnimatorGroup fadeInDown(Component component) {
        AnimatorGroup animatorGroupTongshi = new AnimatorGroup();
        animatorGroupTongshi.runParallel(component.createAnimatorProperty().alphaFrom(0.0f).alpha(1.0f).setDuration(500),
                component.createAnimatorProperty().moveFromY(ComponentUtil.getYInParent(component) - 150).moveToY(ComponentUtil.getYInParent(component)).setDuration(500));
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runSerially(component.createAnimatorProperty().alphaFrom(0.0f).alpha(0.0f).setDuration(100),
                animatorGroupTongshi);
        return animatorGroup;
    }

    private AnimatorGroup fadeInUp(Component component) {
        AnimatorGroup animatorGroupTongshi = new AnimatorGroup();
        animatorGroupTongshi.runParallel(component.createAnimatorProperty().alphaFrom(0.0f).alpha(1.0f).setDuration(500),
                component.createAnimatorProperty().moveFromY(ComponentUtil.getYInParent(component) + 150).moveToY(ComponentUtil.getYInParent(component)).setDuration(500));
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runSerially(component.createAnimatorProperty().alphaFrom(0.0f).alpha(0.0f).setDuration(100),
                animatorGroupTongshi);
        return animatorGroup;
    }

    private AnimatorGroup fadeInLeft(Component component) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runParallel(component.createAnimatorProperty().alphaFrom(0.5f).alpha(1.0f).setDuration(500),
                component.createAnimatorProperty().moveFromX(ComponentUtil.getXInParent(component) - 150).moveToX(ComponentUtil.getXInParent(component)).setDuration(500));
        return animatorGroup;
    }

    private AnimatorGroup fadeInRight(Component component) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runParallel(component.createAnimatorProperty().alphaFrom(0.5f).alpha(1.0f).setDuration(500),
                component.createAnimatorProperty().moveFromX(ComponentUtil.getXInParent(component) + 150).moveToX(ComponentUtil.getXInParent(component)).setDuration(500));
        return animatorGroup;
    }

    private AnimatorGroup rotateIn(Component component) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runParallel(component.createAnimatorProperty().alphaFrom(1.0f).alpha(1.0f),
                component.createAnimatorProperty().rotate(360).setDuration(500));
        return animatorGroup;
    }

    Animation(int animId) {
        this.animId = animId;
    }

    public int getAnimId() {
        return animId;
    }

    /**
     * 获取动画集合对象
     *
     * @param component 需要执行动画的控件
     * @return 动画集合对象
     */
    public AnimatorGroup getAnim(Component component) {
        switch (this.animId) {
            case 1:
                return bounceIn(component);
            case 2:
                return fadeIn(component);
            case 3:
                return fadeInDown(component);
            case 4:
                return fadeInUp(component);
            case 5:
                return fadeInLeft(component);
            case 6:
                return fadeInRight(component);
            case 7:
                return rotateIn(component);
            default:
                return null;
        }
    }

}
